﻿/*
    This file is part of News Worm.

    News Worm is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    News Worm is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with News Worm.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using Microsoft.Win32;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace NewsWorm {
    internal partial class PreferencesDialog : Form {
        public PreferencesDialog( RegistryManager manager, List<Feed> Feeds, IFeedManager fmanager ) {
            InitializeComponent();
            m_Manager = manager;
            m_FeedManager = fmanager;
            numericUpDownArticleCount.Value = Properties.Settings.Default.ArticlesToFetch;
            numericUpDownRefreshFeed.Value = Properties.Settings.Default.FeedRefreshInterval;
            checkBoxSaveWinPos.Checked = Properties.Settings.Default.SaveWindowPosition;
            checkBoxLastExportPath.Checked = Properties.Settings.Default.RememberLastExportPath;
            checkBoxAddedFeedUrls.Checked = Properties.Settings.Default.RememberAddedFeedUrls;
            checkBoxRefreshFeedsOnStartUp.Checked = Properties.Settings.Default.RefreshFeedsOnStartup;
            checkBoxNewArticleNotification.Checked = Properties.Settings.Default.NewArticleNotification;
            checkBoxMinimizeToTray.Checked = Properties.Settings.Default.MinimizeToTray;
            checkBoxStartOnSystem.Checked = Properties.Settings.Default.StartOnLogin;
            checkBoxConfirmFeedRemoval.Checked = m_Manager.ShowRemoveFeedConfirmation;
            checkBoxSpeakNotifications.Checked = Properties.Settings.Default.SpokenNotifications;
            checkBoxActivateScripting.Checked = Properties.Settings.Default.ActivateScripts;
            checkBoxUpdates.Checked = Properties.Settings.Default.CheckForUpdates;

            EnableDisableFeedSelection( checkBoxNewArticleNotification.Checked );
            this.Font = SystemFonts.MessageBoxFont;
            foreach ( Feed feed in Feeds ) {
                checkedListBoxFeeds.Items.Add( feed.Title, feed.HasNotification );
            }
        }

        private RegistryManager m_Manager;
        private IFeedManager m_FeedManager;

        private void buttonOK_Click( object sender, EventArgs e ) {
            Properties.Settings.Default.FeedRefreshInterval = Convert.ToInt32( numericUpDownRefreshFeed.Value );
            Properties.Settings.Default.ArticlesToFetch = Convert.ToInt32( numericUpDownArticleCount.Value );
            Properties.Settings.Default.SaveWindowPosition = checkBoxSaveWinPos.Checked;
            Properties.Settings.Default.RememberLastExportPath = checkBoxLastExportPath.Checked;
            Properties.Settings.Default.RememberAddedFeedUrls = checkBoxAddedFeedUrls.Checked;
            Properties.Settings.Default.RefreshFeedsOnStartup = checkBoxRefreshFeedsOnStartUp.Checked;
            Properties.Settings.Default.NewArticleNotification = checkBoxNewArticleNotification.Checked;
            Properties.Settings.Default.StartOnLogin = checkBoxStartOnSystem.Checked;
            Properties.Settings.Default.MinimizeToTray = checkBoxMinimizeToTray.Checked;
            Properties.Settings.Default.SpokenNotifications = checkBoxSpeakNotifications.Checked;
            Properties.Settings.Default.ActivateScripts = checkBoxActivateScripting.Checked;
            Properties.Settings.Default.CheckForUpdates = checkBoxUpdates.Checked;
            Properties.Settings.Default.Save();
            m_Manager.SetShowRemoveFeedConfirm( checkBoxConfirmFeedRemoval.Checked );
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void EnableDisableFeedSelection(bool value) {
            if ( value ) {
                labelExplain.Enabled = true;
                checkedListBoxFeeds.Enabled = true;
                checkBoxSpeakNotifications.Enabled = true;
            } else {
                labelExplain.Enabled = false;
                checkedListBoxFeeds.Enabled = false;
                checkBoxSpeakNotifications.Enabled = false;
            }
        }

        private void checkBoxNewArticleNotification_CheckedChanged( object sender, EventArgs e ) {
            EnableDisableFeedSelection( checkBoxNewArticleNotification.Checked );
        }

        private void checkedListBoxFeeds_ItemCheck( object sender, ItemCheckEventArgs e ) {

            bool check;
            string title = checkedListBoxFeeds.Items[e.Index].ToString();
            CheckState state = e.NewValue;

            if(state == CheckState.Checked)
                check = true;
            else
                check = false;

            m_FeedManager.GetFeedByTitle( title ).HasNotification = check;

        }

        private void checkBoxStartOnSystem_CheckedChanged( object sender, EventArgs e ) {
            bool WillStart = checkBoxStartOnSystem.Checked;
            SetToStartOnLogin( WillStart );
        }

        private void SetToStartOnLogin(bool value) {
            RegistryKey key = Registry.CurrentUser.OpenSubKey( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true );
            if ( value ) {
                key.SetValue( "NewsWorm", "\"" + Application.ExecutablePath.ToString() + "\"" );
            } else {
                key.DeleteValue( "NewsWorm", false );
            }

            key.Close();
        }

    }
}
